/**
 * 
 */
package main.org.form.action;

import java.util.LinkedHashMap;

import org.compiere.model.MBPMForm;
import org.compiere.model.MBPMProject;
import org.compiere.model.MBPMSubs;
import org.compiere.model.MBPMVersionBudget;
import org.compiere.model.X_BPM_VersionBudget;
import org.compiere.util.Env;
import main.org.model.Budget;
import main.org.model.Utils;


/**
 * @author V.Sokolov
 *
 */
public class Forms extends Budget {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8168009313401256223L;
	
	public static final String sessionKey = "forms";
	protected String page;
	private MBPMForm[] formsLine;
	private int formID;
	private int verbudgetID;
	private int userID;
	private int formCodeID;
	private int subsID;
	X_BPM_VersionBudget budget;
	MBPMSubs subs;
	LinkedHashMap<Integer, String> projectList = new LinkedHashMap<Integer, String>();
	private int defaultProjectID;
	
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	
	public MBPMForm[] getFormsLine() {
		return formsLine;
	}
	public void setFormsLine(MBPMForm[] formsLine) {
		this.formsLine = formsLine;
	}

	public int getFormID() {
		return formID;
	}
	public void setFormID(int formID) {
		this.formID = formID;
	}

	public int getUserID() {
		return userID;
	}
	public void setUserID(int userID) {
		this.userID = userID;
	}
	
	public int getFormCodeID() {
		return formCodeID;
	}
	public void setFormCodeID(int formCodeID) {
		this.formCodeID = formCodeID;
	}

	public int getSubsID() {
		return subsID;
	}
	public void setSubsID(int subsID) {
		this.subsID = subsID;
	}

	public MBPMSubs getSubs() {
		return subs;
	}
	public void setSubs(MBPMSubs subs) {
		this.subs = subs;
	}
	
	public X_BPM_VersionBudget getBudget() {
		return budget;
	}
	
	public void setBudget() {
		this.budget = new MBPMVersionBudget(Env.getCtx(), verbudgetID, null);
//		this.budget = MBPMVersionBudget.getVersionBudget();
	}
	
	public LinkedHashMap<Integer, String> getProjectList() {
		return projectList;
	}
	public void setProjectList() {
		loadProjects();
	}

	public int getDefaultProjectID() {
		return defaultProjectID;
	}
	public void setDefaultProjectID(int defaultProjectID) {
		this.defaultProjectID = defaultProjectID;
	}
	
	public int getVerbudgetID() {
		return verbudgetID;
	}
	
	public void setVerbudgetID(int verbudgetID) {
		this.verbudgetID = verbudgetID;
	}
	
	/* 
	 */
	@Override
	public String execute() throws Exception {
		
		setPage(Utils.TEMPLATE_FIRST);
		StringBuilder err = new StringBuilder();

		Env.setContext(Env.getCtx(), "#AD_Language", getLang());
		Env.setContext(Env.getCtx(), "#AD_User_ID", getUserID());
//		int BPM_VersionBudget_ID = MBPMVersionBudget.getVersionBudget().getBPM_VersionBudget_ID();
		
		setFormsLine(MBPMForm.getForms(verbudgetID));
		if(formsLine.length == 0)
			err.append("Нет форм с настройками на данный бюджетный календарь <br />");
		
		setBudget();
		
		if(getBudget().getBPM_VersionBudget_ID() == 0)
			err.append("Не найден текущий бюджетный календарь на год <br />");
		
		setSubs(new MBPMSubs(Env.getCtx(), MBPMSubs.getSubsID(getUserID()), null));
		setSubsID(getSubs().getBPM_Subs_ID());

		setProjectList();
		if(getProjectList().size() == 0)
			err.append("Не найден рабочий проект <br />");
		
		if(getSubsID() == 0){
			err.append("Неверно настроен справочник Орг. единиц <br />");
			if(getUserID() == 0)
				err.append("Не найден ID пользователя или головная организация<br />");
		}
		
		setMessageError(err.toString());
		if(err.length() == 0){
			session.put(Forms.sessionKey, this);
			return SUCCESS;
		}
		else
			return NONE;

	}
	
	private void loadProjects(){
		
		MBPMProject[] project = MBPMProject.getWorkProject(Env.getCtx(), getBudget().getBPM_VersionBudget_ID(), null);
		if(project.length > 0)
			setDefaultProjectID(project[0].getBPM_Project_ID());
		for(MBPMProject p: project){
			this.projectList.put(p.getBPM_Project_ID(), p.getName());
		}
		
	}

}
